package com.jushu.storbox.view.mdmview;

import java.io.File;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.view.View;
import android.widget.Toast;

import com.jushu.storbox.R;
import com.jushu.storbox.util.LogUtil;
import com.jushu.storbox.util.PublicTools;

public class DocumentDao extends View implements IBaseDao {

	private final static String TAG = "OfficeDao";
		
	public DocumentDao(Context context, String filePath) {
		super(context);
		// TODO Auto-generated constructor stub
		LogUtil.i(TAG, "OfficeDao filePath:" + filePath);
		openDocument(filePath);
	}

	private void openDocument(String filePath) {
		// TODO Auto-generated method stub
		File file = new File(filePath);
    	if(!file.exists())
    	{
    		Toast.makeText(getContext(), 
    				getContext().getString(R.string.allfile_filefolder_no_exist), 
    				Toast.LENGTH_SHORT).show();
    		return ;
    	}
		String fileName = file.getName();
    	if(fileName.startsWith("."))
    	{
    		Toast.makeText(getContext(), 
    				getContext().getString(R.string.file_name_is_invalied), 
    				Toast.LENGTH_SHORT).show();
    		return ;
    	}
		Intent intent = new Intent(Intent.ACTION_VIEW);
        try
        {
    		int lastDotPos = fileName.lastIndexOf(".");
    		if(lastDotPos != -1)
    		{
        		String externName = fileName.substring(lastDotPos);
    			String appType = getOpenFileApp(externName);
	            intent.setDataAndType(Uri.fromFile(new File(filePath)), appType);
	            getContext().startActivity(intent);
    		}
        }
        catch (RuntimeException e)
        {
            intent.setDataAndType(Uri.fromFile(new File(filePath)), "*/*");
            getContext().startActivity(intent);
                                  // FusionField.currentActivity.getText(R.string.osc003_open_file_choose)));
        }
        finally
        {
            PublicTools.finishActivity(getContext());
        }
	}
	
	private String getOpenFileApp(String externName) 
	{
		if (externName == null || externName.equals("") || externName.equals("."))
        {
            return "";
        }
		String type = "*/*";
        externName = externName.toLowerCase();
        for (int i = 0; i < PublicTools.MIME_MapTable.length; i++) {
			if (externName.equals(PublicTools.MIME_MapTable[i][0])){
				type = PublicTools.MIME_MapTable[i][1];
			}
		}
        return type;
	}

	@Override
	public void onDestory() {
		// TODO Auto-generated method stub
	}

	@Override
	public void onStop() {
		// TODO Auto-generated method stub
	}

	@Override
	public void onResume() {
		// TODO Auto-generated method stub
	}
}
